package com.boco.blch.runner.login;

import java.io.IOException;

import com.trilead.ssh2.Connection;
import com.trilead.ssh2.Session;

/**
 * 包含4个数据：主机IP，端口，登陆用户名，登陆密码，缺一不可
 * @author someone
 *
 */
public class UserInfo {
	private Connection conn;
	private String hostName;
	private Integer port;
	private String userName;
	private String password;
	public UserInfo(String ip, Integer port, String userName, String password){
		this.hostName = ip;
		this.port = port;
		this.userName = userName;
		this.password = password;
	}
	
	
	public String getHostName() {
		return hostName;
	}
	public String getUserName() {
		return userName;
	}
	public String getPassword() {
		return password;
	}
	public Integer getPort() {
		return port;
	}


	public Connection login() throws IOException {
		if(conn != null){
			return conn;
		}
		
		conn = new Connection(hostName, port);
		conn.connect();

		boolean isAuthenticated = conn.authenticateWithPassword(userName, password);

		if (isAuthenticated == false){
			conn.close();
			throw new IOException("验证失败！请检查账号和密码是否正确");
		}

		return conn;
	}

	public void logout() throws IOException{
		if(conn == null)
			throw new IOException("您还未登陆成功，无需退出！");
		if(conn != null)
			conn.close();
	}
}
